/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.ui.component.tabel.models;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.opu.db_vdumper.beans.Selectable;
import org.opu.db_vdumper.ui.View;

/**
 *
 * @author Potochnak Yana
 */
public abstract class Model<T extends View> implements Selectable{

    /** If current element selected */
    protected boolean selected = false;

    /** If current element have error */
    protected boolean haveError = false;

    /** If current element have warning */
    protected boolean haveWarning = false;

    /** views of curent model */
    protected Set<T> views = new HashSet<T>();

    /**
     *  Return true if views selected
     * @return true if views selected
     */
    @Override
    public boolean isSelected() {
        return selected;
    }

    /**
     *  Set views selected
     * @param selected true if views must be selected
     */
    @Override
    public void setSelected(boolean selected) {
        this.selected = selected;
        repaint();
    }

    /**
     *  Return true if views have error
     * @return true if views have error
     */
    public boolean isHaveError() {
        return haveError;
    }

    /**
     *  Set views have error
     * @param haveError true if views must be have error
     */
    public void setHaveError(boolean haveError) {
        this.haveError = haveError;
        repaint();
    }

    /**
     *  Return true if views have warning
     * @return true if views have warning
     */
    public boolean isHaveWarning() {
        return haveWarning;
    }

    /**
     *  Set views have warning
     * @param haveWarning true if views must be warned
     */
    public void setHaveWarning(boolean haveWarning) {
        this.haveWarning = haveWarning;
        repaint();
    }

    public abstract boolean hasChildrens();

    public Collection<Model> getChildrens(){
        return new ArrayList<Model>();
    }

    /**
     *  Return number of views
     * @return number of views
     */
    public int viewsCount() {
        return views.size();
    }

    /**
     *  Remove all Views
     */
    public void removeAllViews() {
            views.clear();
        }

    /**
     *  Return true if view added. View can not be added
     * if it already exists in view collection
     * @param view new view
     * @return true if view added
     */
    public boolean addViews(T view) {
            return views.add(view);
        }

    /**
     * Repaint all views
     */
    public void repaint() {
        Iterator<T> i = views.iterator();
        while (i.hasNext()) {
            T view = i.next();
            view.reset();
        }

    }

}
